package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.dto.DrugDto;
import com.woniu.entity.Drug;
import com.woniu.mapper.DrugMapper;
import com.woniu.service.DrugService;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zcy
 * @Create: 2025/11/1 17:03
 */
@Service
public class DrugServiceImpl implements DrugService {
    @Autowired
    private DrugMapper drugMapper;
    @Override
    public ResponseUtil findAll(DrugDto drugDto) {
        Integer pageNum = drugDto.getPageNum();
        Integer pageSize = drugDto.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        List<Drug> all = drugMapper.findAll(drugDto);
        PageInfo<Drug> drugPageInfo = new PageInfo<>(all);
        return ResponseUtil.get(ResponseEnum.OK,drugPageInfo);
    }

    @Override
    public ResponseUtil add(Drug drug) {
        int insert = drugMapper.insert(drug);
        return insert > 0 ? ResponseUtil.get(ResponseEnum.OK): ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil update(Drug drug) {
        int update = drugMapper.updateByPrimaryKeySelective(drug);
        return update > 0 ? ResponseUtil.get(ResponseEnum.OK): ResponseUtil.get(ResponseEnum.FAIL);

    }

    @Override
    public ResponseUtil delete(Long id) {
        int delete = drugMapper.deleteByPrimaryKey(id);
        return delete > 0 ? ResponseUtil.get(ResponseEnum.OK): ResponseUtil.get(ResponseEnum.FAIL);

    }

    @Override
    public ResponseUtil findDrugs() {
        List<Drug> all = drugMapper.findAll(null);
        return ResponseUtil.get(ResponseEnum.OK,all);
    }
}
